package com.example.mac.yuejian14.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mac.yuejian14.activity.MatchDetailActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import com.example.mac.yuejian14.R;

import com.example.mac.yuejian14.bean.MatchBean;
public class MatchListAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;

    private LayoutInflater mLayoutInflaterm;
    public MatchListAdapter(Context mContext,List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflaterm = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflaterm.from(parent.getContext())
                .inflate(R.layout.cardview_match,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MatchBean entity = (MatchBean) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.matchname.setText(entity.getMatname());
        String match_pic = "http://10.0.2.2/neu12_yuejian/Public/Uploads/photo/"+entity.getMatpic();
        Picasso.with(mContext).load(match_pic).into(viewHolder.imagematch);
        if (viewHolder.imagematch.getDrawable()==null){
            viewHolder.imagematch.setImageResource(R.drawable.no_pic);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("matid",entity.getMatid());
                bundle.putString("matname",entity.getMatname());
                bundle.putInt("matcost",entity.getMatcost());
                bundle.putString("mattime",entity.getMattime());
                bundle.putString("matchaddress",entity.getMataddress());
                Intent intent=new Intent(mContext,MatchDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {

        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imagematch;
        TextView matchname;
        public ViewHolder(View itemView){
            super(itemView);
            matchname = (TextView) itemView.findViewById(R.id.match_name);
            imagematch = (ImageView) itemView.findViewById(R.id.match_pic);

        }
    }
}
